package org.reliablesource.prism.gef.editor.model;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.ui.IEditorInput;
import org.reliablesource.prism.core.IPrismScreen;
import org.reliablesource.prism.core.IPrismTutorial;
import org.reliablesource.prism.core.IPrismUnitAttribute;
import org.reliablesource.prism.gef.editor.PrismScreenEditorInput;

/**
 *
 *  PrismGEFAdapterFactory.java
 *  Copyright (c) 2006, Reliable Source, Inc. All Rights Reserved
 *
 *	Created on: Apr 25, 2006 
 *  @author Dennis Park <a href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 *
 */

public class PrismGEFAdapterFactory implements IAdapterFactory {

	private static PrismGEFAdapterFactory instance; 
	private final static Class[] adapterList = { IEditorInput.class, IAdaptable.class };
	private static Map unitAdapterMap;
	private static Map nodeAdapterMap;
	private static Map attrAdapterMap;
	
	static{
		unitAdapterMap = new HashMap();
		nodeAdapterMap = new HashMap();
		attrAdapterMap = new HashMap();
		instance = new PrismGEFAdapterFactory();
	}
	
	private PrismGEFAdapterFactory(){		
	}
	
	public static PrismGEFAdapterFactory getInstance(){
		return instance;
	}
	
	public Object getAdapter(Object adaptableObject, Class adapterType) {
		if(adapterType.getName().equals(IEditorInput.class.getName())){
			Object o = null;
			if(adaptableObject instanceof IPrismScreen){
				if(unitAdapterMap.containsKey(String.valueOf(adaptableObject.hashCode())))
					o = unitAdapterMap.get(String.valueOf(adaptableObject.hashCode()));
				else{
					o = new PrismScreenEditorInput((IPrismScreen) adaptableObject);			
					unitAdapterMap.put(String.valueOf(adaptableObject.hashCode()), o);
				}
				return o;
			}
			else if(adaptableObject instanceof IPrismTutorial){
				if(nodeAdapterMap.containsKey(String.valueOf(adaptableObject.hashCode())))
					o = nodeAdapterMap.get(String.valueOf(adaptableObject.hashCode()));
				else{
					o = new PrismScreenEditorInput((IPrismScreen) adaptableObject);			
					nodeAdapterMap.put(String.valueOf(adaptableObject.hashCode()), o);
				}
				return o;
			}
			else if(adaptableObject instanceof IPrismUnitAttribute){
				if(attrAdapterMap.containsKey(String.valueOf(adaptableObject.hashCode())))
					o = attrAdapterMap.get(String.valueOf(adaptableObject.hashCode()));
				else{
					o = new PrismScreenEditorInput((IPrismScreen) adaptableObject);			
					attrAdapterMap.put(String.valueOf(adaptableObject.hashCode()), o);
				}
				return o;
			}
			
		}
		return null;
	}

	public Class[] getAdapterList() {
		return adapterList;
	}

}
